rm(list = setdiff(ls(), lsf.str())) #remove all unnecessary files from workspace except functions

load("Study 1/Altered Data/Study1_Swiss_Household15.RData")
#SI A.9: Swiss Household Panel results----------
swiss.15<-list()
swiss.15[[1]] <- glm(vote_svp15 ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age + language2 + language3, data=data15, family=binomial)
swiss.15[[2]]  <- glm(vote_svp15 ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age +language2 + language3 + edu2 + edu3 + edu4 + edu5 + edu6 + edu7 + edu8 + edu9 + income + income_mis + zero1(lr_placement), data=data15, family=binomial)
labels <- c("Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Female", "Age" , "Language: (Ref: French) German", "Language: Italian", "2: Lower secondary", "3A: Upper secondary", "3B: Upper secondary", "3C: Upper secondary", "4: Post-secondary education", "5A: First stage of tertiary education", "5A: First stage of tertiary education", "6: Second stage of tertiary education ", "Income", "Income missing (0-1)", "Left-right ideology")
stargazer2(swiss.15, odd.ratio=TRUE, title="Swiss Household Panel 2015: Vote for Swiss People's Party", align=TRUE, omit.stat=c("LL","ser","f", "adj.rsq"), star.cutoffs=c(0.05), 
           covariate.labels = labels, dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "tiny",column.labels = c("Base", "Figure 1"),  notes = "Odds ratios with standard errors from logistic regression models; *p<0.05",  notes.append = FALSE, out="Tables/Swiss15_results.tex", no.space=TRUE, label="tab:Swiss15_results" )


#SI A.9 Correlation----------------------
myvars <- c("agre", "open", "con", "ext", "neu" , "lr_placement")
cor_ivs <- data15[myvars]

names(cor_ivs) <- c("1. Agreeableness","2. Opennesss","3. Conscientiousness",
                    "4. Extraversion", "5. Neuroticism", 
                    "6. Left-Right Ideology")

correlation.matrix <- cor(cor_ivs, use="complete.obs")
correlation.matrix <- data.frame(get_lower_tri(correlation.matrix))
correlation.matrix<-correlation.matrix[,c(1:5)]
names(correlation.matrix) <- seq(1,5,1)
correlation.matrix<-as.matrix(correlation.matrix)
stargazer(correlation.matrix, title="Swiss Household Panel 2015: Correlation Matrix of Independent Variables", out="Tables/Swiss15_cor.tex", no.space=TRUE, label="tab:Swiss_cor15", digits=2)

#SI A.9 Factor loadings------------
latent<-(with(data15, data.frame(a1_rude_rec,a2_forgive, a3_kind, o1_original,o2_artistic, o3_imagination, c2_lazy,c1_thorough, c3_efficient, n1_worry,n2_nervous,  n3_relaxed_rec, e1_talkative,e2_sociable, e3_reserved_rec)))
latent <- na.omit(latent)
CFA_agree <-'Agree = ~ a1_rude_rec+a2_forgive+ a3_kind
Open = ~ o1_original+o2_artistic+ o3_imagination
Con = ~ c2_lazy+c1_thorough+ c3_efficient
Ext = ~ e1_talkative+e2_sociable+ e3_reserved_rec
Neu = ~ n1_worry+n2_nervous+  n3_relaxed_rec
Agree ~~ 1*Agree
Open ~~ 1*Open
Con ~~ 1*Con
Ext ~~ 1*Ext
Neu ~~ 1*Neu
' 
fit<-cfa(CFA_agree, data=latent)
p<-parameterEstimates(fit, standardized=TRUE) %>%  dplyr::select(std.all, pvalue)
p <- p[1:15, ] 
names(p) <- c("Standardized Factor Loading", "p-value")
(setattr(p, "row.names", c("Agreeableness item 1", "Agreeableness item 2", "Agreeableness item 3", "Openness item 1", "Openness item 2", "Openness item 3", "Conscientiousness item 1", "Conscientiousness item 2", "Conscientiousness item 3",  "Extraversion item 1", "Extraversion item 2", "Extraversion item 3", "Neuroticism item 1", "Neuroticism item 2", "Neuroticism item 3")))

p<-xtable(caption = "Swiss Household Panel 2015: Big Five Standardized Factor Loadings", label = "tab:Swiss15_cfa", p)
print(p, type="latex", file="Tables/Swiss15_cfa.tex", size="tiny", caption.placement="top")


#SI A.9 Descriptive statistics-----
desc.labels <- c("Populist vote", labels)
stargazer(swiss.15[[2]]$model, covariate.labels=desc.labels, type = "latex", summary.stat = c("mean", "sd", "N", "median","min",  "max"), out="Tables/Swiss_descrip15.tex", title="Descriptive statistics Swiss Household Panel 2015", no.space=TRUE, label="tab:Swiss_descriptives15", digits=2)

#Alpha Agreeableness
alpha_agre<-with(data15,data.frame(a1_rude_rec,a2_forgive, a3_kind))
psych::alpha(alpha_agre)

#Alpha Openness
alpha<-with(data15,data.frame(o1_original,o2_artistic, o3_imagination))
psych::alpha(alpha)

#Alpha Conscientiousness
alpha<-with(data15,data.frame(c2_lazy,c1_thorough, c3_efficient))
psych::alpha(alpha)

#Alpha Extraversion
alpha<-with(data15,data.frame(e1_talkative,e2_sociable, e3_reserved_rec))
psych::alpha(alpha)

#Alpha Neuroticism
alpha<-with(data15,data.frame(n1_worry,n2_nervous, n3_relaxed_rec))
psych::alpha(alpha)